package py.com.alfa.escriba.gui.rest.services.resttemplate;

import java.io.IOException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;

import py.com.alfa.escriba.gui.config.EscribaConfigApplicationContextProvider;
import py.com.alfa.escriba.gui.dto.IntercepcionLongDTO;
import py.com.alfa.escriba.gui.dto.admin.ClienteDTO;
import py.com.alfa.escriba.gui.dto.admin.ClienteInfoDTO;
import py.com.alfa.escriba.gui.dto.admin.DomicilioDTO;
import py.com.alfa.escriba.gui.dto.admin.EscribanosDTO;
import py.com.alfa.escriba.gui.dto.admin.PersonaDTO;
import py.com.alfa.escriba.gui.dto.admin.PersonaInfoDTO;
import py.com.alfa.escriba.gui.dto.admin.PersonaInfoParaControlDTO;
import py.com.alfa.escriba.gui.dto.admin.TelefonoDTO;
import py.com.alfa.escriba.gui.dto.query.PersonaQueryDTO;
import py.com.alfa.escriba.gui.rest.Headers;
import py.com.alfa.escriba.gui.rest.services.AdminService;
import py.com.alfa.escriba.gui.services.Services;
import py.com.alfa.escriba.gui.utils.ObjectUtils;
import py.com.sif.rest.client.spring.request.ClientRequestBuilder;

@SuppressWarnings({ "unchecked", "unused"})
public class AdminServiceClient implements AdminService {

	private Logger log = LoggerFactory.getLogger(getClass());
	
	public AdminServiceClient() {}

	@Override
	public List<ClienteDTO> obtenerClientes() throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		ResponseEntity<ClienteDTO[]> response = (ResponseEntity<ClienteDTO[]>) request.exchange(LIST_CLIENTES, HttpMethod.GET, ClienteDTO[].class);
		log.info("OK - obtenerClientes");
		List<ClienteDTO> result =request.getArrayAsList(response.getBody());
		return result;
	}

	@Override
	public ClienteInfoDTO obtenerCliente(Long id) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", id.toString());
		ResponseEntity<ClienteInfoDTO> response = (ResponseEntity<ClienteInfoDTO>) request.exchange(GET_CLIENTE, HttpMethod.GET, ClienteInfoDTO.class);
		log.info("OK - obtenerCliente {}", id);
		ClienteInfoDTO result = response.getBody();
		return result;
	}

	@Override
	public void actualizarCliente(ClienteDTO dto) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addBody(dto);
		ResponseEntity<ClienteDTO> response = (ResponseEntity<ClienteDTO>) request.exchange(UPD_CLIENTE, HttpMethod.PUT, ClienteDTO.class);
		log.info("OK - actualizarCliente");
		ClienteDTO result = response.getBody();
		dto.setTrn(result.getTrn());
	}

	@Override
	public void eliminarCliente(Long id) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
	    if(!ObjectUtils.isEmpty(id))
	        request.addHeader(Headers.ID_CLIENTE, id.toString());
	    	    
	    request.exchange(DEL_CLIENTE, HttpMethod.DELETE, null);
		log.info("OK - eliminarCliente");
	}

	@Override
	public void crearCliente(ClienteDTO dto) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addBody(dto);
		ResponseEntity<ClienteDTO> response = (ResponseEntity<ClienteDTO>) request.exchange(NEW_CLIENTE, HttpMethod.POST, ClienteDTO.class);
		log.info("OK - crearCliente");
		ClienteDTO result = response.getBody();
		dto.setTrn(result.getTrn());
		dto.setId(result.getId());
	}

	@Override
	public void eliminarEmpleoCliente(IntercepcionLongDTO dto) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addBody(dto);
		request.exchange(DEL_EMPLEO, HttpMethod.PUT, null);
		log.info("OK - eliminarEmpleoCliente");
	}

	@Override
	public void crearEmpleoCliente(IntercepcionLongDTO dto) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addBody(dto);
		request.exchange(NEW_EMPLEO, HttpMethod.POST, null);
		log.info("OK - crearEmpleoCliente");
	}

	@Override
	public void actualizarTelefono(TelefonoDTO dto) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addBody(dto);
		request.exchange(UPD_TELEFONO, HttpMethod.PUT, null);
		log.info("OK - actualizarTelefono");
	}

	@Override
	public void eliminarTelefono(TelefonoDTO dto) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addBody(dto);
		request.exchange(DEL_TELEFONO, HttpMethod.PUT, null);
		log.info("OK - actualizarTelefono");		
	}

	@Override
	public void crearTelefono(TelefonoDTO dto) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addBody(dto);
		ResponseEntity<TelefonoDTO> response = (ResponseEntity<TelefonoDTO>) request.exchange(NEW_TELEFONO, HttpMethod.PUT, TelefonoDTO.class);
		log.info("OK - crearTelefono");
		TelefonoDTO result = response.getBody();
	}

	@Override
	public void actualizarDomicilio(DomicilioDTO dto) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addBody(dto);
		ResponseEntity<DomicilioDTO> response = (ResponseEntity<DomicilioDTO>) request.exchange(UPD_DOMICILIO, HttpMethod.PUT, DomicilioDTO.class);
		log.info("OK - actualizarDomicilio");
		DomicilioDTO result = response.getBody();
		dto.setTrn(result.getTrn());
	}

	@Override
	public void eliminarDomicilio(Long id) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
	    if(!ObjectUtils.isEmpty(id))
	        request.addHeader(Headers.ID_DOMICILIO, id.toString());
	    	    
	    request.exchange(DEL_DOMICILIO, HttpMethod.DELETE, null);
		log.info("OK - eliminarDomicilio");
	}

	@Override
	public void crearDomicilio(DomicilioDTO dto) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addBody(dto);
		ResponseEntity<DomicilioDTO> response = (ResponseEntity<DomicilioDTO>) request.exchange(NEW_CLIENTE, HttpMethod.POST, DomicilioDTO.class);
		log.info("OK - crearDomicilio");
		DomicilioDTO result = response.getBody();
		dto.setTrn(result.getTrn());
		dto.setId(result.getId());
	}

	@Override
	public ClienteInfoDTO crearClienteInfo(ClienteInfoDTO dto) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addBody(dto);
		ResponseEntity<ClienteInfoDTO> response = (ResponseEntity<ClienteInfoDTO>) request.exchange(NEW_CLIENTEINFO, HttpMethod.POST, ClienteInfoDTO.class);
		log.info("OK - crearClienteInfo");
		ClienteInfoDTO result = response.getBody();
		return result;
	}

	@Override
	public void actualizarClienteInfo(ClienteInfoDTO dto) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addBody(dto);
		request.exchange(UPD_CLIENTEINFO, HttpMethod.PUT, ClienteInfoDTO.class);
		log.info("OK - actualizarClienteInfo");
	}
	
	@Override
	public PersonaInfoDTO crearPersonaInfo(PersonaInfoDTO dto) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addBody(dto);
		ResponseEntity<PersonaInfoDTO> response = (ResponseEntity<PersonaInfoDTO>) request.exchange(NEW_PERSONAINFO, HttpMethod.POST, PersonaInfoDTO.class);
		log.info("OK - crearPersonaInfo");
		PersonaInfoDTO result = response.getBody();
		return result;
	}

	@Override
	public EscribanosDTO crearEscribano(EscribanosDTO dto) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addBody(dto);
		ResponseEntity<EscribanosDTO> response = (ResponseEntity<EscribanosDTO>) request.exchange(NEW_ESCRIBANO, HttpMethod.POST, EscribanosDTO.class);
		log.info("OK - crearEscribano");
		EscribanosDTO result = response.getBody();
		return result;
	}	
	
	@Override
	public PersonaDTO obtenerPersona(Long id) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", id.toString());
		ResponseEntity<PersonaDTO> response = (ResponseEntity<PersonaDTO>) request.exchange(GET_PERSONA, HttpMethod.GET, PersonaDTO.class);
		log.info("OK - obtenerPersona");
		PersonaDTO result = response.getBody();
		return result;
	}
	
	@Override
	public EscribanosDTO obtenerEscribano(Long id) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", id.toString());
		ResponseEntity<EscribanosDTO> response = (ResponseEntity<EscribanosDTO>) request.exchange(GET_ESCRIBANO, HttpMethod.GET, EscribanosDTO.class);
		log.info("OK - obtenerEscribano");
		EscribanosDTO result = response.getBody();
		return result;
	}

	@Override
	public PersonaDTO crearPersona(PersonaDTO dto) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addBody(dto);
		ResponseEntity<PersonaDTO> response = (ResponseEntity<PersonaDTO>) request.exchange(CREA_PERSONA, HttpMethod.POST, PersonaDTO.class);
		log.info("OK - crearPersona");
		PersonaDTO result = response.getBody();
		dto.setId(result.getId());
		return result;
	}

	@Override
	public void actualizarPersonaInfo(PersonaInfoDTO dto) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addBody(dto);
		request.exchange(UPD_PERSONAINFO, HttpMethod.PUT, PersonaInfoDTO.class);
		log.info("OK - actualizarPersonaInfo");
	}

	@Override
	public List<PersonaDTO> obtenerPersonas(PersonaDTO dto) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
//		request.addBody(dto);
		ResponseEntity<PersonaDTO[]> response = (ResponseEntity<PersonaDTO[]>) request.exchange(LIST_PERSONAS, HttpMethod.GET, PersonaDTO[].class);
		log.info("OK - obtenerPersonas");
		List<PersonaDTO> result =request.getArrayAsList(response.getBody());
		return result;
	}
	
	@Override
	public List<PersonaInfoDTO> buscarPersonas(PersonaDTO dto) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addBody(dto);
		ResponseEntity<PersonaInfoDTO[]> response = (ResponseEntity<PersonaInfoDTO[]>) request.exchange(BUSCAR_PERSONA, HttpMethod.POST, PersonaInfoDTO[].class);
		log.info("OK - obtenerPersonas");
		List<PersonaInfoDTO> result =request.getArrayAsList(response.getBody());
		return result;
	}
	
	@Override
	public PersonaInfoDTO obtenerPersonaInfo(Long id) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", id.toString());
		ResponseEntity<PersonaInfoDTO> response = (ResponseEntity<PersonaInfoDTO>) request.exchange(GET_PERSONAINFO, HttpMethod.GET, PersonaInfoDTO.class);
		log.info("OK - obtenerPersonaInfo");
		PersonaInfoDTO result = response.getBody();
		return result;
	}

	@Override
	public void eliminarPersona(Long id) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
	    if(!ObjectUtils.isEmpty(id))
	        request.addHeader(Headers.ID_PERSONA, id.toString());
	    	    
	    request.exchange(DEL_PERSONA, HttpMethod.DELETE, null);
		log.info("OK - eliminarPersona");
	}
	
	@Override
	public void eliminarPersonaInfo(Long id) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
	    if(!ObjectUtils.isEmpty(id))
	        request.addHeader(Headers.ID_PERSONA, id.toString());
	    	    
	    request.exchange(DEL_PERSONAINFO, HttpMethod.DELETE, null);
		log.info("OK - eliminarPersonaInfo");
	}

	@Override
	public List<EscribanosDTO> listEscribanos() throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		ResponseEntity<EscribanosDTO[]> response = (ResponseEntity<EscribanosDTO[]>) request.exchange(LIST_ESCRIBANOS, HttpMethod.GET, EscribanosDTO[].class);
		log.info("OK - obtenerEscribanos");
		List<EscribanosDTO> result =request.getArrayAsList(response.getBody());
		return result;
	}

	@Override
	public PersonaInfoParaControlDTO getPersonaByPaisDocumento(PersonaQueryDTO values) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addBody(values);
		ResponseEntity<PersonaInfoParaControlDTO> response = (ResponseEntity<PersonaInfoParaControlDTO>) request.exchange(PER_BY_PAIS_DOC, HttpMethod.POST, PersonaInfoParaControlDTO.class);
		log.info("OK - crearPersonaInfo");
		PersonaInfoParaControlDTO result = response.getBody();
		return result;
	}


}
